package cn.njyd.corejava.thread.myday03;

class StudyType{

    private int flag = 1;

    public synchronized void watchVideo(int i) throws InterruptedException {
        while (flag!=1) {
            this.wait();
        }
        //具备权限
        System.out.println(Thread.currentThread().getName()+"第"+i+"个学习视频");
        //设置标志位，并通知写代码线程
        flag = 2;
        this.notifyAll();
    }
    public synchronized void writerCode(int n) throws InterruptedException {
        while (flag != 2) {
            this.wait();
        }
        System.out.println(Thread.currentThread().getName()+"使用IDEA写第"+n+"个程序");
        flag = 3;
        this.notifyAll();
    }

    public synchronized void writerSummary(int n) throws InterruptedException {
        while (flag != 3) {
            this.wait();
        }
        System.out.println(Thread.currentThread().getName()+"正在写第"+n+"个知识点总结");
        flag = 1;
        this.notifyAll();

    }

}


public class ThreadWaitNotifyDemo03 {
    public static void main(String[] args) {
        StudyType studyType = new StudyType();
        Thread t1 = new Thread(()->{
            for (int i=1;i<=20;i++) {
                final int n = i;
                try {
                    studyType.watchVideo(n);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"张四同学");

        Thread t2 = new Thread(()->{
            for (int i=1;i<=20;i++) {
                final int n = i;
                try {
                    studyType.writerCode(n);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"张五同学");

        Thread t3 = new Thread(()->{
            for (int i=1;i<=20;i++) {
                final int n = i;
                try {
                    studyType.writerSummary(n);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"张三同学");
        t1.start();
        t2.start();
        t3.start();
    }
}
